/*
 * Created on Nov 25, 2008 Copyright (c) Sybase, Inc. 2008 All rights reserved.
 */

package edu.cmu.cs.webapp.ioms.logging;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import edu.cmu.cs.webapp.ioms.utils.SWTUtils;

/**
 * @author Tian(Christian) Qiu
 */
public class DontShowAgainDialog extends MessageDialog {
	private String _preferenceKey;
	private Button _dontShowAgainButton;
	private IPreferenceStore _preferenceStore;

	/**
	 * @param parentShell
	 * @param dialogTitle
	 * @param prefStore
	 * @param preferenceKey
	 * @param dialogTitleImage
	 * @param dialogMessage
	 * @param dialogImageType
	 * @param dialogButtonLabels
	 * @param defaultIndex
	 */
	public DontShowAgainDialog(Shell parentShell, String dialogTitle,
			IPreferenceStore prefStore, String preferenceKey,
			Image dialogTitleImage, String dialogMessage, int dialogImageType,
			String[] dialogButtonLabels, int defaultIndex) {
		super(parentShell, dialogTitle, dialogTitleImage, dialogMessage,
				dialogImageType, dialogButtonLabels, defaultIndex);
		this._preferenceKey = preferenceKey;
		this._preferenceStore = prefStore;

	}

	/**
	 * Convenience method to open a standard error dialog.
	 * 
	 * @param parent
	 *            the parent shell of the dialog, or <code>null</code> if none
	 * @param title
	 *            the dialog's title, or <code>null</code> if none
	 * @param store
	 *            the store put the prompt preference
	 * @param key
	 *            the key of the prompt preference
	 * @param message
	 *            the message
	 */

	public static void openError(Shell parent, String title,
			IPreferenceStore store, String key, String message) {
		DontShowAgainDialog dialog = new DontShowAgainDialog(parent, title,
				store, key, null, // accept
				// the
				// default
				// window
				// icon
				message, ERROR, new String[] { "OK" }, 0); // ok
		// is
		// the
		// default
		if (!dialog.needToShow()) {
			return;
		}

		dialog.open();
		return;
	}

	/**
	 * Convenience method to open a standard information dialog.
	 * 
	 * @param parent
	 *            the parent shell of the dialog, or <code>null</code> if none
	 * @param title
	 *            the dialog's title, or <code>null</code> if none
	 * @param store
	 *            the store put the prompt preference
	 * @param key
	 *            the key of the prompt preference
	 * @param message
	 *            the message
	 */
	public static void openInformation(Shell parent, String title,
			IPreferenceStore store, String key, String message) {
		DontShowAgainDialog dialog = new DontShowAgainDialog(parent, title,
				store, key, null, // accept
				// the
				// default
				// window
				// icon
				message, INFORMATION, new String[] { "OK" }, 0);
		// ok is the default

		if (!dialog.needToShow()) {
			return;
		}

		dialog.open();
		return;
	}

	/**
	 * Convenience method to open a simple confirm (OK/Cancel) dialog.
	 * 
	 * @param parent
	 *            the parent shell of the dialog, or <code>null</code> if none
	 * @param title
	 *            the dialog's title, or <code>null</code> if none
	 * @param store
	 *            the store put the prompt preference
	 * @param key
	 *            the key of the prompt preference
	 * @param message
	 *            the message
	 * @return <code>true</code> if the user presses the OK button,
	 *         <code>false</code> otherwise
	 */
	public static boolean openConfirm(Shell parent, String title,
			IPreferenceStore store, String key, String message) {
		DontShowAgainDialog dialog = new DontShowAgainDialog(parent, title,
				store, key, null, // accept
				// the
				// default
				// window
				// icon
				message, QUESTION, new String[] { "OK", "Cancel" }, 0); // OK is
																		// the
		// default is ok

		// always return true if no need to show the dialog
		if (!dialog.needToShow()) {
			return true;
		}

		return dialog.open() == 0;
	}

	/**
	 * Convenience method to open a simple question (Yes/No) dialog.
	 * 
	 * @param parent
	 *            the parent shell of the dialog, or <code>null</code> if none
	 * @param title
	 *            the dialog's title, or <code>null</code> if none
	 * @param store
	 *            the store put the prompt preference
	 * @param key
	 *            the key of the prompt preference
	 * @param message
	 *            the message
	 * @return <code>true</code> if the user presses the OK button,
	 *         <code>false</code> otherwise
	 */
	public static boolean openQuestion(Shell parent, String title,
			IPreferenceStore store, String key, String message) {
		DontShowAgainDialog dialog = new DontShowAgainDialog(parent, title,
				store, key, null, // accept
				// the
				// default
				// window
				// icon
				message, QUESTION, new String[] { "OK", "Cancel" }, 0); // OK is
																		// the
		// default is ok

		// always return true if no need to show the dialog
		if (!dialog.needToShow()) {
			return true;
		}

		return dialog.open() == 0;
	}

	/**
	 * Convenience method to open a standard warning dialog.
	 * 
	 * @param parent
	 *            the parent shell of the dialog, or <code>null</code> if none
	 * @param title
	 *            the dialog's title, or <code>null</code> if none
	 * @param store
	 *            the store put the prompt preference
	 * @param key
	 *            the key of the prompt preference
	 * @param message
	 *            the message
	 */
	public static void openWarning(Shell parent, String title,
			IPreferenceStore store, String key, String message) {
		DontShowAgainDialog dialog = new DontShowAgainDialog(parent, title,
				store, key, null, // accept
				// the
				// default
				// window
				// icon
				message, WARNING, new String[] { "OK" }, 0); // ok
		// is
		// the
		// default

		if (!dialog.needToShow()) {
			return;
		}
		dialog.open();
		return;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.dialogs.MessageDialog#createCustomArea(org.eclipse.
	 * swt.widgets.Composite)
	 */
	@Override
	protected Control createCustomArea(Composite parent) {
		Composite container = SWTUtils.createComposite(parent, 1);
		this._dontShowAgainButton = SWTUtils.createCheckBox(container,
				"Don't show again.", 1);
		return container;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.dialogs.MessageDialog#buttonPressed(int)
	 */
	@Override
	public void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			this._preferenceStore.setValue(this._preferenceKey,
					this._dontShowAgainButton.getSelection());
		}
		super.buttonPressed(buttonId);
	}

	/**
	 * @return whether the dialog should be prompted
	 */
	private boolean needToShow() {
		return !this._preferenceStore.getBoolean(this._preferenceKey);
	}

}
